import display
try:
    from gi.repository import Gtk
    assert display.gtk_version() == 3
except:
    import gtk as Gtk
    assert display.gtk_version() == 2
from project import Project, File, Changeset
import os
import pango

class StatusWindow:
    text_cell = Gtk.CellRendererText()
    number_cell = Gtk.CellRendererText()

    def __init__(self):
        self.text_cell.set_property('weight', pango.WEIGHT_HEAVY)
        self.number_cell.set_property('weight', pango.WEIGHT_HEAVY)
        self.number_cell.set_property('foreground', 'red')
        self.number_cell.set_property('xalign', 0.5)

        builder = display.load_ui("status.glade")
        self.window = builder.get_object("window")
        self.window.connect("delete-event", self.hide)

        self.refresh_button = builder.get_object("refresh")
        self.add_button = builder.get_object("add")
        self.remove_button  = builder.get_object("remove")
        self.info_button = builder.get_object("info")

        self.auto = builder.get_object("auto")
        self.interval = builder.get_object("interval")

        self.project_store = builder.get_object("project_store")
        self.project_view = builder.get_object("project_view")
        self.selection = builder.get_object("selection")


        for index, title in {0 : "Project", 1 : "Branch", 2 : "Uncommited", 3: "Unpushed"}.iteritems():
            if index > 1:
                cell = self.number_cell
            else:
                cell = self.text_cell
            column = Gtk.TreeViewColumn(title, cell, text=index)
            column.set_cell_data_func(cell, self.coloriser, None)
            column.set_sort_column_id(index)
            self.project_view.append_column(column)

    def coloriser(self, column, cell, model, row, user_data):
        if model.get_value(row, 2) > 0 or model.get_value(row, 3) > 0:
            cell.set_property('weight-set', True)
            cell.set_property('foreground-set', True)
        else:
            cell.set_property('weight-set', False)
            cell.set_property('foreground-set', False)


    def show(self, settings):
        window_size = settings.status_size
        self.window.set_default_size(window_size.width, window_size.height)
        self.auto.set_active(settings.auto)
        self.interval.set_value(settings.interval)
        self.window.show()
        
    def hide(self, widget, event):
        self.window.hide()
        return True

    def add_project(self, project):
        self.project_store.append(project.get_row())

    def remove_project(self, treeiter):
        self.project_store.remove(treeiter)

    def get_project(self, path):
        for row in self.project_store:
            if row[4] == path:
                return row.iter
        return None

    def update_project(self, project):
        values = project.get_row()
        row = self.get_project(project.path)
        for column in range(1, 4):
            self.project_store.set_value(row, column, values[column])


class StatusDetail:
    text_renderer = Gtk.CellRendererText()

    def __init__(self, project, settings):
        builder = display.load_ui("detail.glade")
        self.window = builder.get_object("window")
        self.pane = builder.get_object("pane")
        self.close_button = builder.get_object("close")
        self.close_button.connect("clicked", self.button_close)
        self.window.connect("delete-event", self.window_close)

        self.file_view = builder.get_object("file_view")
        self.file_store = builder.get_object("file_store")

        window_size = settings.detail_size
        self.window.set_default_size(window_size.width, window_size.height)
        self.pane.set_position(settings.detail_pane)

        for index, title in {0 : "Status", 1 : "Path"}.iteritems():
            column = Gtk.TreeViewColumn(title, self.text_renderer, text=index)
            self.file_view.append_column(column)
        for afile in project.files:
            self.file_store.append(afile.get_row())

        self.changeset_view = builder.get_object("changeset_view")
        self.changeset_store = builder.get_object("changeset_store")
        for index, title in {0 : "Date", 1 : "Summary"}.iteritems():
            column = Gtk.TreeViewColumn(title, self.text_renderer, text=index)
            self.changeset_view.append_column(column)
        for changeset in project.changesets:
            self.changeset_store.append(changeset.get_row())

        self.window.set_title(project.name)
        self.window.show()

    def button_close(self, button):
        self.window.destroy()

    def window_close(self, widget, event):
        self.window.destroy()


class StatusTray:
    def __init__(self):
        menu = Gtk.Menu()
        self.show_item = Gtk.MenuItem("Show Projects")
        self.show_item.show()
        menu.append(self.show_item)
        self.refresh_now_item = Gtk.MenuItem("Refresh Now")
        self.refresh_now_item.show()
        menu.append(self.refresh_now_item)
        self.quit_item = Gtk.MenuItem("Quit")
        self.quit_item.show()
        menu.append(self.quit_item)
        menu.show()

        self.tray = display.load_tray(menu)

    def update(self, count):
        print("unclean " + str(count))
        self.tray.update(count)


